import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  UseGuards,
  ParseIntPipe,
  DefaultValuePipe,
  Query,
  Request,
  Req,
} from '@nestjs/common';
import { LoggerService } from 'src/common/logger/logger.service';
import { CreateAdminDto } from 'src/dto/create-admin.dto';
import { AdminLoginDto } from 'src/dto/login-admin.dto';
import { UpdateAdminDto } from 'src/dto/update-admin.dto';
import { AdminGuard } from 'src/guards/admin.guard';
import { AdminService } from 'src/service/admin.service';

@Controller('admin/admin')
export class AdminController {
  constructor(
    private readonly adminService: AdminService,
    private readonly logger: LoggerService,
  ) {}

  @UseGuards(AdminGuard)
  @Post()
  create(@Body() createAdminDto: CreateAdminDto) {
    return this.adminService.create(createAdminDto);
  }

  @UseGuards(AdminGuard)
  @Get()
  findAll(
    @Query('pageNum', new DefaultValuePipe(1), ParseIntPipe) pageNum: number,
    @Query('pageSize', new DefaultValuePipe(10), ParseIntPipe) pageSize: number,
  ) {
    return this.adminService.findAll(pageNum, pageSize);
  }

  @UseGuards(AdminGuard)
  @Get('/adminInfo')
  async adminInfo(@Request() req) {
    let uid = req.uid;
    this.logger.log('uid', uid);
    return this.adminService.findOne(+uid); // 获取用户信息
  }

  @UseGuards(AdminGuard)
  @Get('/menus')
  menus(@Req() req) {
    let uid = req.uid;
    return this.adminService.menus(+uid);
  }

  @UseGuards(AdminGuard)
  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.adminService.findOne(+id);
  }

  @UseGuards(AdminGuard)
  @Patch(':id')
  update(@Param('id') id: string, @Body() updateAdminDto: UpdateAdminDto) {
    return this.adminService.update(+id, updateAdminDto);
  }

  @UseGuards(AdminGuard)
  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.adminService.remove(+id);
  }

  @Post('/login')
  login(@Body() adminLoginDto: AdminLoginDto) {
    return this.adminService.login(adminLoginDto);
  }

  @UseGuards(AdminGuard)
  @Post('/resetPassword')
  resetPassword(@Body('id') id: string) {
    return this.adminService.resetPassword(+id);
  }

  @UseGuards(AdminGuard)
  @Post('/bindPermissions')
  bindPermissions(
    @Body('adminId') adminId: string,
    @Body('permissions') permissions: any,
  ) {
    return this.adminService.bindPermissions(+adminId, permissions);
  }


}
